#Boa:Frame:Frame_Servicos
# -*- coding: cp1252 -*-

import wx


def create(parent):
    return Frame_Servicos(parent)

[wxID_FRAME_SERVICOS, wxID_FRAME_SERVICOSBACKTOMENU, 
 wxID_FRAME_SERVICOSCADASTRARSERVICO, wxID_FRAME_SERVICOSCHOICE1, 
 wxID_FRAME_SERVICOSEXCLUIRSERVICO, wxID_FRAME_SERVICOSLISTCTRL1, 
 wxID_FRAME_SERVICOSPESQUISARSERVICO, wxID_FRAME_SERVICOSSTATICBITMAP1, 
 wxID_FRAME_SERVICOSSTATICBOX1, wxID_FRAME_SERVICOSSTATICTEXT1, 
 wxID_FRAME_SERVICOSTEXTCTRL_PESQUISAR, 
] = [wx.NewId() for _init_ctrls in range(11)]

class Frame_Servicos(wx.Frame):
    def _init_coll_listCtrl1_Columns(self, parent):
        # generated method, don't edit

        parent.InsertColumn(col=0, format=wx.LIST_FORMAT_LEFT, heading=u'Nome',
              width=-1)
        parent.InsertColumn(col=1, format=wx.LIST_FORMAT_LEFT,
              heading=u'Pre\xe7o', width=-1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME_SERVICOS, name=u'Frame_Servicos',
              parent=prnt, pos=wx.Point(536, 81), size=wx.Size(665, 647),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Frame_Servicos')
        self.SetClientSize(wx.Size(649, 609))
        self.SetBackgroundColour(wx.Colour(255, 255, 255))
        self.Center(wx.BOTH)

        self.staticBox1 = wx.StaticBox(id=wxID_FRAME_SERVICOSSTATICBOX1,
              label=u"Cristina's Hair - Cadastro de Servi\xe7os",
              name='staticBox1', parent=self, pos=wx.Point(16, 16),
              size=wx.Size(608, 371), style=0)
        self.staticBox1.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))

        self.CadastrarServico = wx.Button(id=wxID_FRAME_SERVICOSCADASTRARSERVICO,
              label=u'Cadastrar Novo Servi\xe7o', name=u'CadastrarServico',
              parent=self, pos=wx.Point(32, 56), size=wx.Size(128, 56),
              style=0)
        self.CadastrarServico.Bind(wx.EVT_BUTTON, self.OnCadastrarServicoButton,
              id=wxID_FRAME_SERVICOSCADASTRARSERVICO)

        self.textCtrl_Pesquisar = wx.TextCtrl(id=wxID_FRAME_SERVICOSTEXTCTRL_PESQUISAR,
              name=u'textCtrl_Pesquisar', parent=self, pos=wx.Point(184, 80),
              size=wx.Size(192, 21), style=wx.TE_PROCESS_ENTER, value=u'')
        self.textCtrl_Pesquisar.Bind(wx.EVT_TEXT_ENTER,
              self.OnTextCtrl_PesquisarTextEnter,
              id=wxID_FRAME_SERVICOSTEXTCTRL_PESQUISAR)

        self.choice1 = wx.Choice(choices=[], id=wxID_FRAME_SERVICOSCHOICE1,
              name='choice1', parent=self, pos=wx.Point(400, 80),
              size=wx.Size(96, 21), style=0)
        self.choice1.SetLabel(u'')
        self.choice1.Bind(wx.EVT_CHOICE, self.OnChoice1Choice,
              id=wxID_FRAME_SERVICOSCHOICE1)

        self.PesquisarServico = wx.Button(id=wxID_FRAME_SERVICOSPESQUISARSERVICO,
              label=u'Pesquisar', name=u'PesquisarServico', parent=self,
              pos=wx.Point(512, 80), size=wx.Size(80, 24), style=0)
        self.PesquisarServico.Bind(wx.EVT_BUTTON, self.OnPesquisarServicoButton,
              id=wxID_FRAME_SERVICOSPESQUISARSERVICO)

        self.ExcluirServico = wx.Button(id=wxID_FRAME_SERVICOSEXCLUIRSERVICO,
              label=u'Excluir Servi\xe7o', name=u'ExcluirServico', parent=self,
              pos=wx.Point(32, 136), size=wx.Size(128, 56), style=0)
        self.ExcluirServico.Bind(wx.EVT_BUTTON, self.OnExcluirServicoButton,
              id=wxID_FRAME_SERVICOSEXCLUIRSERVICO)

        self.backtomenu = wx.Button(id=wxID_FRAME_SERVICOSBACKTOMENU,
              label=u'Voltar para o Menu', name=u'backtomenu', parent=self,
              pos=wx.Point(32, 288), size=wx.Size(128, 56), style=0)
        self.backtomenu.Bind(wx.EVT_BUTTON, self.OnBacktomenuButton,
              id=wxID_FRAME_SERVICOSBACKTOMENU)

        self.staticText1 = wx.StaticText(id=wxID_FRAME_SERVICOSSTATICTEXT1,
              label=u'Pesquisar Servi\xe7os', name='staticText1', parent=self,
              pos=wx.Point(184, 48), size=wx.Size(126, 19), style=0)
        self.staticText1.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Times New Roman'))

        self.staticBitmap1 = wx.StaticBitmap(bitmap=wx.Bitmap(u'Imagens/logotipo300dpi-30%.jpg',
              wx.BITMAP_TYPE_JPEG), id=wxID_FRAME_SERVICOSSTATICBITMAP1,
              name='staticBitmap1', parent=self, pos=wx.Point(74, 416),
              size=wx.Size(500, 176), style=0)
        self.staticBitmap1.Center(wx.HORIZONTAL)

        self.listCtrl1 = wx.ListCtrl(id=wxID_FRAME_SERVICOSLISTCTRL1,
              name='listCtrl1', parent=self, pos=wx.Point(184, 128),
              size=wx.Size(408, 208), style=wx.LC_REPORT)
        self._init_coll_listCtrl1_Columns(self.listCtrl1)
        self.listCtrl1.Bind(wx.EVT_LIST_ITEM_ACTIVATED,
              self.OnListCtrl1ListItemActivated,
              id=wxID_FRAME_SERVICOSLISTCTRL1)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.parent = parent
        self.choice1.Append('Nome')
        self.choice1.Append('Preco')
        self.choice1.SetSelection(0)
        self.RedimensionarColunas(-2)
        self.OnPesquisarServicoButton(None)


    def OnBacktomenuButton(self, event):
        self.Close()        

    #Funcao ativada ao pressionar ENTER na caixa de Pesquisa
    def OnTextCtrl_PesquisarTextEnter(self, event):
        self.OnPesquisarServicoButton(event)

    #Funcao do Botao Cadastrar Servico
    def OnCadastrarServicoButton(self, event):
        from Dialog_CadastrarServico import *
        janela = CadastrarServico(self)
        janela.ShowModal()

    #Funcao do Botao Pesquisar Servico
    def OnPesquisarServicoButton(self, event):

        from Class_ControleServico import *
        Controle = ControleServico()
        
        #Pega o valor do campo da pesquisa e o Campo a ser pesquisado (Nome, Preco)
        Dado = self.textCtrl_Pesquisar.GetValue()
        Campo = self.choice1.GetStringSelection()
        
        #Pesquisa todos os servicos que possuam pelo menos uma parte do 
        #dados a ser pesquisado igual ao que foi digitado na caixa de pesquisa
        self.ListaServicos = Controle.PesquisarServicos(Dado, Campo)
        
        #Envia todos os dados da ListaServicos pra o metodo que preenche a tabela
        self.PreencherTabela(self.ListaServicos)

    #Funcao do Botao Excluir Servico
    def OnExcluirServicoButton(self, event):
        from Class_ControleServico import *
        Controle = ControleServico()
        
        if self.listCtrl1.GetItemCount() <> 0:
            
            #Descobre o Servico selecionado
            pos = self.listCtrl1.GetFocusedItem()
            Servico =  self.ListaServicos[pos]
            
            
            #Mostra a MessageBox confirmando a exclusao do Servico
            m = "Voce tem certeza que deseja excluir o Servico\n%s ??"%(Servico.GetNome())
            t = "Exclusao Servico"
            confirmacao = wx.MessageBox(m,t,wx.YES_NO)
            
            #Se "YES"
            if confirmacao == 2:
                
                
                confirmacao = Controle.RemoverServico(Servico)
                
                #Se o Servico foi removido
                if confirmacao == True:
                    m = "Servico removido com sucesso!"
                    t = "Exclusao Servico"
                    wx.MessageBox(m,t,wx.OK)
                    
            
        #Se Nenhum Servico estiver selecionado
        else:
            m = "Nenhum Servico na lista"
            t = "Exclusao Servico"
            confirmacao = wx.MessageBox(m,t,wx.ICON_ERROR)
            
        
    #Funcao do Duplo Clique em alguma linha da tabela
    def OnListCtrl1ListItemActivated(self, event):
        from Dialog_EditarServico import *
   
        #Descobre o Servico selecionado
        pos = self.listCtrl1.GetFocusedItem()
        Servico =  self.ListaServicos[pos]
        
        #Criar o Dialog Editar Servico passando o Objeto Servico a ser editado
        Janela = EditarServico(self, Servico)
        Janela.ShowModal()
        
        
    #Metodo que Preenche a tabela com os nomes e precos dos produtos pesquisados
    def PreencherTabela(self, ListaServicos):
        import sys
        lista = self.listCtrl1
        
        #Deleta todos os item da tabela
        lista.DeleteAllItems()
        
        if ListaServicos <> False:
            #Pega os dados de cada Servico e joga na tabela
            for i in ListaServicos:
                
                index = lista.InsertStringItem(sys.maxint,'0')
                if (float(index)/2).is_integer() == False:
                    self.listCtrl1.SetItemBackgroundColour(index, (230,230,230))
                lista.SetStringItem(index, 0, str(i.GetNome()))
                lista.SetStringItem(index, 1, str('R$ '+i.GetPreco()))
                
            self.RedimensionarColunas(-3)

    #Metodo que Redimensiona o tamanho das colunas
    def RedimensionarColunas(self, num):
        self.listCtrl1.SetColumnWidth(0, num)
        self.listCtrl1.SetColumnWidth(1, num)

    def OnChoice1Choice(self, event):
        self.textCtrl_Pesquisar.SetFocus()
